package com.myplans.client.presenter;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.myplans.client.Presenter;
import com.myplans.client.UserUtil;
import com.myplans.client.event.ViewPlanEvent;
import com.myplans.client.service.PlanServiceAsync;
import com.myplans.client.service.UserServiceAsync;
import com.myplans.client.view.BaseView;
import com.myplans.client.view.custom.PlanPanel;
import com.myplans.shared.Plan;
import com.myplans.shared.User;

public class SearchPresenter implements Presenter {

	public interface Display {

		public Widget asWidget();
		
		public FlowPanel getPlansPanel();
		
		public void setSearchString(String searchString);
		
	}

	private HandlerManager eventBus;
	private Display display;
	private UserServiceAsync userServiceAsync;
	private PlanServiceAsync planService;
	private String searchString;

	public SearchPresenter(HandlerManager eventBus, Display display) {
		this.eventBus = eventBus;
		this.display = display;
		this.bind();
	}

	public void go(HasWidgets container) {
		((BaseView)container).getCentralPanel().clear();
		((BaseView)container).getCentralPanel().add(display.asWidget());
		((BaseView)container).getSearchTextBox().setText("");
		display.setSearchString(searchString);
		getPlans();
	}
	
	public void getPlans() {
		display.getPlansPanel().clear();
		
		userServiceAsync.getPlansForUser(UserUtil.user, new AsyncCallback<ArrayList<Plan>>() {
			
			@Override
			public void onSuccess(ArrayList<Plan> result) {
				final ArrayList<Plan> userPlans = result;
				planService.getPlans(null, searchString, new AsyncCallback<List<Plan>>() {
					
					@Override
					public void onSuccess(List<Plan> result) {
						for (Plan plan : result) {
							final PlanPanel panel = new PlanPanel(plan, userPlans.contains(plan));
							display.getPlansPanel().add(panel);
							panel.getTitleLabel().addClickHandler(new ClickHandler() {
								
								@Override
								public void onClick(ClickEvent event) {
									eventBus.fireEvent(new ViewPlanEvent(panel.getPlan()));
								}
							});
							panel.getAttendButton().addClickHandler(new ClickHandler() {
								
								@Override
								public void onClick(ClickEvent event) {
									if (panel.isAttending() == true) {
										userServiceAsync.deletePlanForUser(panel.getPlan(), UserUtil.user
												, new AsyncCallback<User>() {
												
													@Override
													public void onSuccess(User result) {
														panel.setAttending(false);
													}
												
													@Override
													public void onFailure(Throwable caught) {
														Window.alert("Could not de-attend this plan");
													}
										});
									} else {
										userServiceAsync.createPlanForUser(panel.getPlan(), UserUtil.user
												, new AsyncCallback<User>() {
												
													@Override
													public void onSuccess(User result) {
														panel.setAttending(true);
													}
												
													@Override
													public void onFailure(Throwable caught) {
														Window.alert("Could not attend this plan");
													}
										});
									}
								}
							});
						}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Could not get plans for user222");
			}
		});
		
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	private void bind() {
	}

	public void setUsersService(UserServiceAsync userServiceAsync) {
		this.userServiceAsync = userServiceAsync;
	}

	public void setPlanService(PlanServiceAsync planServiceAsync) {
		this.planService = planServiceAsync;
	}
	
	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}


}
